// FindFormView.cpp : implementation file
//

#include "stdafx.h"
#include "codehelp.h"
#include "FindFormView.h"
#include "CHAdo.h"
#include "CHTreeCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFindFormView

IMPLEMENT_DYNCREATE(CFindFormView, CFormView)

CFindFormView::CFindFormView()
	: CFormView(CFindFormView::IDD)
{
	m_hBrush = NULL;

	//{{AFX_DATA_INIT(CFindFormView)
	//}}AFX_DATA_INIT
}

CFindFormView::~CFindFormView()
{
}

void CFindFormView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFindFormView)
	DDX_Control(pDX, IDC_BTN_FIND, m_find);
	DDX_Control(pDX, IDC_EDIT1, m_edit);
	DDX_Control(pDX, IDC_STATIC_INFO, m_info);
	DDX_Control(pDX, IDC_LIST1, m_list);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFindFormView, CFormView)
	//{{AFX_MSG_MAP(CFindFormView)
	ON_WM_CTLCOLOR()
	ON_WM_SIZE()
	ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkList1)
	ON_BN_CLICKED(IDC_BTN_FIND, OnBtnFind)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFindFormView diagnostics

#ifdef _DEBUG
void CFindFormView::AssertValid() const
{
	CFormView::AssertValid();
}

void CFindFormView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

HBRUSH CFindFormView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	UNREFERENCED_PARAMETER(pWnd);
	UNREFERENCED_PARAMETER(nCtlColor);
	
	if (m_hBrush != NULL)
		::DeleteObject(m_hBrush);

	m_hBrush = ::CreateSolidBrush(RGB(255,255,255));

	pDC->SetBkColor(RGB(255, 255, 255));
	pDC->SetTextColor(RGB(0,0,0));

	return m_hBrush;
}

void CFindFormView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	m_find.SetBitmaps(IDB_FIND, RGB(255, 255, 255));
	m_find.SetTooltipText("xxxxx");
	m_find.DrawTransparent(FALSE);
	m_find.SetFlat(TRUE);

	m_info.SetTextColor(RGB(0, 0, 0));
	m_info.SetFontName("Tahoma");
	m_info.SetFontSize(9);
	m_info.SetTransparent(TRUE);
	m_info.SetAutoSize(TRUE);

	m_edit.SetFont(&CHGlobalData.font, TRUE);

	m_list.SubclassHeader();
	m_imageList.Create(1, 17, ILC_COLOR24|ILC_MASK, 2, 2);
	m_list.SetImageList(&m_imageList, LVSIL_SMALL);
	m_list.SetExtendedStyle(m_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT);

	m_list.AddColumn("xxxxxxx", 160);
}

void CFindFormView::OnSize(UINT nType, int cx, int cy) 
{
	if( m_list.GetSafeHwnd())
	{
		const int nMarginBottom = 4;
		const int nMarginX = 4;
		const int nMarginY = 4;
		const int nButtonX = 60;
		const int nButtonY = 18;

		CRect rc, rcCtrl;

		GetClientRect(&rc);
		ClientToScreen(&rc);

		m_list.GetClientRect(&rcCtrl);
		m_list.ClientToScreen(&rcCtrl);
		if(rc.Height() < 100)
		{
			m_list.ShowWindow(FALSE);
		}
		else
		{
			m_list.ShowWindow(TRUE);
			m_list.MoveWindow(nMarginX, 
							  nMarginY*2 + nButtonY*2, 
							  rc.Width() - nMarginX * 2,
							  rc.Height() - nMarginY*3 - nButtonY*2);
			m_list.SetColumnWidth(0, rc.Width() - nMarginX * 2 - 8);
		}

		CWnd* pWnd = GetDlgItem(IDC_EDIT1);
		pWnd->MoveWindow(nMarginX, nMarginY + nButtonY, 
						  rc.Width() - nMarginX * 2,
						  nButtonY+2);

		m_find.MoveWindow(rc.Width() - nButtonX - nMarginX*2 + 3, 
				nMarginY, nButtonX, nButtonY);
		m_find.GetClientRect(&rcCtrl);
		m_find.ClientToScreen(&rcCtrl);

		CRect rcInfo;
		m_info.GetClientRect(&rcInfo);
		m_info.ClientToScreen(&rcInfo);
		m_info.ShowWindow(rcCtrl.left > rcInfo.right);
		m_info.MoveWindow(nMarginX+2, nMarginY+2, 
						  rcInfo.Width(),
						  rcInfo.Height());
	}
}

BOOL CFindFormView::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message == WM_SYSCHAR &&(pMsg->wParam == 'F' || pMsg->wParam == 'f'))
	{
		Find();
		return TRUE;
	}

	if(pMsg->message == WM_KEYDOWN &&
		pMsg->wParam == VK_RETURN && pMsg->hwnd == GetDlgItem(IDC_EDIT1)->GetSafeHwnd())
	{
		Find();
		return TRUE;
	}

	return CFormView::PreTranslateMessage(pMsg);
}

void CFindFormView::Find()
{
	CString strKey;
	GetDlgItem(IDC_EDIT1)->GetWindowText(strKey);

	if(strKey.IsEmpty())
	{
		AfxMessageBox("strKey.IsEmpty()");
		return;
	}

	CWaitCursor wait;
	m_list.DeleteAllItems();
	CHAdo ado(theApp.conn, theApp.rset);
	try
	{
		CString sql;
		strKey.Replace("\"", "");
		sql.Format("SELECT NodeId, Title FROM TTree WHERE [Type] = 1 AND Title like \"%%%s%%\" ORDER BY NodeId", strKey);
		ado.Select(sql);

		while(!ado.GetRecordset()->adoEOF)
		{
			int nIndex = m_list.InsertItem(m_list.GetItemCount(), ado.GetText(1));
			m_list.SetItemData(nIndex, ado.GetLong(0));
			ado.GetRecordset()->MoveNext();
		}
	}
	catch(_com_error& e)
	{
		ado.ShowError(e);
	}
}

void CFindFormView::OnDblclkList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	POSITION pos = m_list.GetFirstSelectedItemPosition();
	if (pos != NULL)
	{
		int nItem = m_list.GetNextSelectedItem(pos);
		int nNodeId = m_list.GetItemData(nItem);
		theQueue.RemoveAll();
		theQueue.AddTail(nNodeId);

		CString str;
		CHAdo ado(theApp.conn, theApp.rset);
		try
		{
			while(true)
			{
				str.Format("SELECT ParentId FROM TTree WHERE NodeId=%d", nNodeId);
				ado.Select(str);
				if(ado.GetRecordset()->adoEOF) return;
				nNodeId = ado.GetLong(0);

				if(nNodeId != 0)
					theQueue.AddTail(nNodeId);
				else
					break;
			}
			NMHDR nm;
			nm.hwndFrom = GetSafeHwnd();
			nm.idFrom = 0;
			nm.code = CH_TREE_FIND;
			theApp.m_pMainWnd->SendMessage(WM_NOTIFY, 0, (LPARAM)&nm);
		}
		catch(_com_error& e)
		{
			ado.ShowError(e);
		}
	}

	*pResult = 0;
}

void CFindFormView::OnBtnFind() 
{
	if(theApp.conn->State)
		Find();
	else
		AfxMessageBox("!theApp.conn->State");
}
